﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DAO
{
    public class DataProvider
    {
        //private const string ConnectionString = @"isostore:/Database.sdf";
        private const string ConnectionString = @"Data Source=appdata:/dataDic.sdf; File Mode = read only";
        protected  DataContext context = null;

        public DataProvider()
        {
            context = new DataContext(ConnectionString);
        }

        public static void CreateDatabase()
        {

            using (var context = new DataContext(ConnectionString))
            {
                if (!context.DatabaseExists())
                {
                    //create database if it does not exist
                    context.CreateDatabase();
                }
            }
        }
        public static void MoveReferenceDatabase()
        {
            // Obtain the virtual store for the application.
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();

            // Create a stream for the file in the installation folder.
            using (Stream input = Application.GetResourceStream(new Uri("dataDic.sdf", UriKind.Relative)).Stream)
            {
                // Create a stream for the new file in isolated storage.
                using (IsolatedStorageFileStream output = iso.CreateFile("dataDic.sdf"))
                {
                    // Initialize the buffer.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the file from the installation folder to isolated storage. 
                    while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        output.Write(readBuffer, 0, bytesRead);
                    }
                }
            }
        }
        /**
        * Chức năng của hàm
        *
        * @param $Tên Tham số:
        *   mô tả tham số
         * @return
        *   Kết quả trả về
        */
        public static void DeleteDatabase()
        {
            using (var context = new DataContext(ConnectionString))
            {
                if (context.DatabaseExists())
                {
                    //delete database if it exist
                    context.DeleteDatabase();
                }
            }
        }

    }
}
